package test;

import common.Utils;
import org.openqa.selenium.By;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-07
 * Time:14:41
 */
public class RegisterPage extends Utils {
    public static String url ="http://81.70.248.108:58080/sign-up.html";

    public RegisterPage() {
        super(url);
    }

    /**
     * 检查页面元素是否加载
     */
    public void checkRegister(){
        webDriver.findElement(By.cssSelector("#username"));
        webDriver.findElement(By.cssSelector("#nickname"));
        webDriver.findElement(By.cssSelector("#password"));
        webDriver.findElement(By.cssSelector("#passwordRepeat"));
        webDriver.findElement(By.cssSelector("#policy"));
        webDriver.findElement(By.cssSelector("#submit"));

        //检查页面是否跳转正确
        String title = webDriver.getTitle();
        //检查注册框下文字是否正确
        String text = webDriver.findElement(By.xpath("/html/body/div/div/div[2]")).getText();
        assert title.equals("论坛系统-用户注册"):"页面跳转失败";
        assert text.equals("我已有一个账户? 登录"):"注册框下文本出现问题";
        System.out.println(text);
    }

    public void registerFail(){
        //获取操作元素
        By username = By.cssSelector("#username");
        By nickname = By.cssSelector("#nickname");
        By password = By.cssSelector("#password");
        By passwordRepeat = By.cssSelector("#passwordRepeat");
        By policy = By.cssSelector("#policy");
        By submit = By.cssSelector("#submit");
        //1.输入选项为空
        //1.1用户名不为空，其余为空
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();
        webDriver.findElement(username).sendKeys("测试用户1");


        webDriver.findElement(policy);
        webDriver.findElement(submit).click();

        //1.2 昵称不为空，其余为空
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();

        webDriver.findElement(nickname).sendKeys("测试用户1");

        webDriver.findElement(policy);
        webDriver.findElement(submit).click();


        //1.3 密码不为空，其余为空
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();

        webDriver.findElement(password).sendKeys("123456");

        webDriver.findElement(policy);
        webDriver.findElement(submit).click();

        //1.4 确认密码不为空，其余为空
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();

        webDriver.findElement(passwordRepeat).sendKeys("123456");

        webDriver.findElement(policy);
        webDriver.findElement(submit).click();

        //1.5 勾选协议，其他为空
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();

        webDriver.findElement(policy).click();
        webDriver.findElement(submit).click();

        //2.两次密码不一致
        //初始状态
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();
        webDriver.findElement(policy).click();

        webDriver.findElement(username).sendKeys("测试用户1");
        webDriver.findElement(nickname).sendKeys("测试用户1");
        webDriver.findElement(password).sendKeys("123456");
        webDriver.findElement(passwordRepeat).sendKeys("12345");

        webDriver.findElement(policy).click();
        webDriver.findElement(submit).click();


        //3. 用户名重复
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();
        webDriver.findElement(policy).click();

        webDriver.findElement(username).sendKeys("测试用户");
        webDriver.findElement(nickname).sendKeys("测试用户1");
        webDriver.findElement(password).sendKeys("123456");
        webDriver.findElement(passwordRepeat).sendKeys("123456");

        webDriver.findElement(policy).click();
        webDriver.findElement(submit).click();

    }
    public void registerSuc(String username1,String password1){
        //获取操作元素
        By username = By.cssSelector("#username");
        By nickname = By.cssSelector("#nickname");
        By password = By.cssSelector("#password");
        By passwordRepeat = By.cssSelector("#passwordRepeat");
        By policy = By.cssSelector("#policy");
        By submit = By.cssSelector("#submit");
        //初始话之前的状态
        webDriver.findElement(username).clear();
        webDriver.findElement(nickname).clear();
        webDriver.findElement(password).clear();
        webDriver.findElement(passwordRepeat).clear();
        webDriver.findElement(policy).click();

        webDriver.findElement(username).sendKeys(username1);
        webDriver.findElement(nickname).sendKeys("测试用户1");
        webDriver.findElement(password).sendKeys(password1);
        webDriver.findElement(passwordRepeat).sendKeys("123456");

        webDriver.findElement(policy).click();
        webDriver.findElement(submit).click();
    }
}
